﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using CoreRay;
using CoreRay.Client.Controls;

namespace CoreRay.Client
{
    /// <summary>
    /// Interaction logic for PreviewWindow.xaml
    /// </summary>
    public partial class PreviewWindow : Window
    {
        private LogWindow logWindow;
        private Renderer renderer;
        private Scene scene;
        private Dictionary<RenderRegion, RenderRegionControl> currentRenderRegions;

        public PreviewWindow(Renderer renderer, Scene scene)
        {
            InitializeComponent();

            this.logWindow = new LogWindow();
            this.logWindow.Show();

            this.renderer = renderer;
            this.scene = scene;

            this.mainCanvas.Width = 1024;
            this.mainCanvas.Height = 768;

            this.SizeToContent = SizeToContent.WidthAndHeight;

            this.Loaded += new RoutedEventHandler(PreviewWindow_Loaded);
            this.Closed += new EventHandler(PreviewWindow_Closed);
            this.logWindow.IsVisibleChanged += new DependencyPropertyChangedEventHandler(logWindow_IsVisibleChanged);
        }

        void logWindow_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            this.moveButton.IsChecked = (bool) e.NewValue;
        }

        void PreviewWindow_Closed(object sender, EventArgs e)
        {
            this.logWindow.Close();
            Application.Current.Shutdown();
        }

        void PreviewWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.moveButton.IsChecked = true;
            this.SizeToContent = SizeToContent.Manual;
            this.Background = SystemColors.AppWorkspaceBrush;

            this.previewImage.Source = this.renderer.Film.PreviewImage;

            this.currentRenderRegions = new Dictionary<RenderRegion, RenderRegionControl>();

            this.renderer.RenderRegionUpdating += new EventHandler<RenderRegionUpdatedEventArgs>(renderer_RenderRegionUpdating);
            this.renderer.RenderRegionUpdated += new EventHandler<RenderRegionUpdatedEventArgs>(renderer_RenderRegionUpdated);

            renderer.BeginRenderScene(this.scene, new AsyncCallback(RenderCallback), null);
        }

        void renderer_RenderRegionUpdating(object sender, RenderRegionUpdatedEventArgs e)
        {
            this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, (DispatcherOperationCallback) delegate(object arg)
            {
                RenderRegionControl renderRegionControl = new RenderRegionControl();
                this.mainCanvas.Children.Add(renderRegionControl);

                renderRegionControl.Width = e.RenderRegion.Width;
                renderRegionControl.Height = e.RenderRegion.Height;

                Canvas.SetLeft(renderRegionControl, e.RenderRegion.PositionX);
                Canvas.SetTop(renderRegionControl, e.RenderRegion.PositionY);

                this.currentRenderRegions.Add(e.RenderRegion, renderRegionControl);

                return null;
            }, null);
        }

        void renderer_RenderRegionUpdated(object sender, RenderRegionUpdatedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (DispatcherOperationCallback) delegate(object arg)
            {
                if(this.currentRenderRegions.ContainsKey(e.RenderRegion))
                {
                    this.mainCanvas.Children.Remove(this.currentRenderRegions[e.RenderRegion]);
                    this.currentRenderRegions.Remove(e.RenderRegion);
                }

                return null;
            }, null);
        }

        private void RenderCallback(IAsyncResult asyncResult)
        {
            this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, (DispatcherOperationCallback) delegate(object arg)
            {
                this.saveButton.IsEnabled = true;

                return null;
            }, null);
        }

        private void zoomSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.zoomTextBlock.Text = string.Format("{0}%", (int) (this.zoomSlider.Value * 100));
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog saveFileDialog = new Microsoft.Win32.SaveFileDialog();
            saveFileDialog.ShowDialog();
        }

        private void moveButton_Checked(object sender, RoutedEventArgs e)
        {
            this.zoomButton.IsChecked = false;
            this.logWindow.Show();
        }

        private void moveButton_Unchecked(object sender, RoutedEventArgs e)
        {
            this.logWindow.Hide();
        }

        private void zoomButton_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void zoomButton_Unchecked(object sender, RoutedEventArgs e)
        {

        }

        private void mainCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if(e.LeftButton == MouseButtonState.Pressed)
            {
                
            }
        }
    }
}
